package com.linkzd.iot.service.Impl;

import java.util.List;

import com.linkzd.common.core.domain.AjaxResult;
import com.linkzd.common.utils.DateUtils;
import com.linkzd.iot.domain.*;
import com.linkzd.iot.mapper.DeviceDataLogMapper;
import com.linkzd.iot.mapper.DeviceTypeMapper;
//import com.linkzd.iot.utils.mqtt.client.MqttPushClient;
import com.linkzd.iot.service.*;
import com.linkzd.iot.utils.mqtt.client.MqttClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.linkzd.iot.mapper.DeviceInfoMapper;

/**
 * 设备信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-18
 */
@Service
public class DeviceInfoServiceImpl implements IDeviceInfoService 
{
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    @Autowired
    private IDeviceTypeService deviceTypeService;

    @Autowired
    private IDeviceDataLogService deviceDataLogService;

    @Autowired
    private ISensorsReportDataLogService sensorsReportDataLogService;

    /**
     * @Description: TODO mqttPushClient为了调用mqtt的下发接口
     * @Author: cyx
     * @Date: 2024/5/20 22:14
     */
    @Autowired
    private MqttClientUtil mqttPushClient;

    /**
     * @Description: TODO  roomInfoService为了调用房间信息表的房间号
     * @Author: cyx
     * @Date: 2024/5/20 23:00
     */
    @Autowired
    private IRoomInfoService roomInfoService;
    @Autowired
    private IDeviceFeedbackService deviceFeedbackService;
    /**
     * 查询设备信息
     *
     * @param deviceId 设备信息主键
     * @return 设备信息
     */
    @Override
    public DeviceInfo selectDeviceInfoByDeviceId(Long deviceId)
    {
        return deviceInfoMapper.selectDeviceInfoByDeviceId(deviceId);
    }




    /**
     * 查询设备信息
     *
     * @param deviceCode 设备信息主键
     * @return 设备信息
     */
    @Override
    public DeviceInfo selectDeviceInfoByDeviceCode(String deviceCode)
    {
        return deviceInfoMapper.selectDeviceInfoByDeviceCode(deviceCode);
    }

    /**
     * 查询设备信息列表
     * 
     * @param deviceInfo 设备信息
     * @return 设备信息
     */
    @Override
    public List<DeviceInfo> selectDeviceInfoList(DeviceInfo deviceInfo)
    {
        List<DeviceInfo> deviceInfoList = deviceInfoMapper.selectDeviceInfoList(deviceInfo);
        for (DeviceInfo deviceInfo1:deviceInfoList){
            deviceInfo1.setDeviceVariety(deviceInfo1.getType().getDeviceVariety());
        }
        return deviceInfoList;
    }

    /**
     * 新增设备信息
     * 
     * @param deviceInfo 设备信息
     * @return 结果
     */
    @Override
    public int insertDeviceInfo(DeviceInfo deviceInfo)
    {
        deviceInfo.setCreateTime(DateUtils.getNowDate());
        DeviceType type = deviceTypeService.selectDeviceTypeByDeviceTypeId(deviceInfo.getDeviceTypeId());
        RoomInfo room = roomInfoService.selectRoomInfoById(deviceInfo.getRoomId());
        DeviceInfo newInfo = new DeviceInfo();
        newInfo.setRoomId(deviceInfo.getRoomId());
        int bh = selectDeviceInfoList(newInfo).size()+10;
        String devicebh=String.valueOf(room.getRoomNum()).concat(String.valueOf(type.getDeviceType())).concat(String.valueOf(bh));
        deviceInfo.setDeviceCode(devicebh);
        deviceInfo.setDeviceData("0");
        if(type.getDeviceVariety() == 0){
            deviceInfo.setDeviceState(0);
        }
        return deviceInfoMapper.insertDeviceInfo(deviceInfo);
    }

    /**
     * @Description: TODO 平台下发在服务层调用mqtt与硬件连接的接口进行修改数据库
     * @Author: cyx
     * @Date: 2024/5/20 22:16
     * @Param: [deviceInfo]
     * @Return: com.linkzd.common.core.domain.AjaxResult
     */
    
    @Override
    public String changeDeviceSwitch(DeviceInfo deviceInfo) {
        //通过deviceid去设备信息表中查询roomid
        DeviceInfo dInfo = deviceInfoMapper.selectDeviceInfoByDeviceId(deviceInfo.getDeviceId());
        Long roomId = dInfo.getRoomId();
        deviceInfo.setDeviceCode(dInfo.getDeviceCode());
        dInfo.setDeviceData(deviceInfo.getDeviceData());
        //设备下发之前判断设备反馈ok表中是否有垃圾数据，有就改状态
        List<DeviceFeedbackModel> deviceFeedbackModels = deviceFeedbackService.selectDeviceFeedbackList(dInfo.getDeviceCode());
        if(deviceFeedbackModels.size() > 0){
            deviceFeedbackService.updateDeviceFeedback(dInfo.getDeviceCode());
        }
        //通过设备信息表中的roomId去查询对应的房间号
        RoomInfo roomInfo = roomInfoService.selectRoomInfoById(roomId);
        String feedback = mqttPushClient.publishMessage(roomInfo.getRoomNum(), dInfo);
        if("true".equals(feedback)){
            //修改的数据库
            int ret = deviceInfoMapper.updateDeviceInfo(deviceInfo);
//            DeviceDataLog log = new DeviceDataLog();
            deviceInfo.setDeviceState(dInfo.getDeviceState());
            if(deviceDataLogService.insertDeviceDataLog(deviceInfo)>0){
                deviceDataLogService.deleteDeviceDataLogByCreateTime(DateUtils.getNowDate());
                return "ok";
            }
        }
        return feedback;
    }

    /**
     * 修改设备信息
     * 
     * @param deviceInfo 设备信息
     * @return 结果
     */
    @Override
    public int updateDeviceInfo(DeviceInfo deviceInfo)
    {
        deviceInfo.setUpdateTime(DateUtils.getNowDate());
        return deviceInfoMapper.updateDeviceInfo(deviceInfo);
    }



    /**
     * 修改设备信息
     *
     * @param deviceInfo 设备信息
     * @return 结果
     */
    @Override
    public int updateDeviceInfoByCode(DeviceInfo deviceInfo)
    {
        deviceInfo.setUpdateTime(DateUtils.getNowDate());
        return deviceInfoMapper.updateDeviceInfoByCode(deviceInfo);
    }

    /**
     * 批量删除设备信息
     * 
     * @param deviceIds 需要删除的设备信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceInfoByDeviceIds(Long[] deviceIds)
    {
        return deviceInfoMapper.deleteDeviceInfoByDeviceIds(deviceIds);
    }

    /** @Author Wangym
     * @Date 2024/5/23 上午 12:05
     * @Description 一键开关灯
     * @Param
     * @Return
     **/
    @Override
    public int turnLightAll(int state) {
        //
        int count=0;
        //查询设备信息中的所有灯
        List<DeviceInfo> deviceInfos = deviceInfoMapper.selectDeviceInfoOfLights();
        for (DeviceInfo dInfo:deviceInfos){
            dInfo.setDeviceData(String.valueOf(state));
            mqttPushClient.publishMessage(dInfo.getRoom().getRoomNum(),dInfo);
            count += deviceInfoMapper.updateDeviceInfo(dInfo);

        }
        //修改的数据库条数
        return count;

    }

    /** @Author Wangym
     * @Date 2024/5/23 上午 12:09
     * @Description 查询设备信息中所有的灯
     * @Param
     * @Return
     **/
    @Override
    public List<DeviceInfo> selectDeviceInfoOfLights(){
        return deviceInfoMapper.selectDeviceInfoOfLights();
    }

    /**
     * @Author Wangym
     * @Date 2024/5/23 上午 12:08
     * @Description 获取设备操作数量
     * @Param
     * @Return
     **/
    @Override
    public EquipmentOperationsNumModel equipmentOperations() {
        int actuatorDdelivered = deviceDataLogService.selectDeviceDataLogNum();
        int sensorReporting = sensorsReportDataLogService.selectSensorsReportDataLogNum();
        return new EquipmentOperationsNumModel(actuatorDdelivered,sensorReporting);
    }
}
